var BTree = function(text, value){
	var children = {};
	var values = [];
	//var skipList;
	
	this.add = function(text, value){
		if(!text || text == ""){
			values.push(value);
		} else {
			for(var i in children){
				var eq = equalTo(i, text)
				if(eq > 0){
					var node = text.substr(0, eq);
					if(!children[node]) children[node] = new BTree();
					if(i.length != eq){
						var newText = i.slice(eq);
						children[node].addChildren(newText, children[i]);
						delete children[i];
					}
					if(text.length != eq){
						var newText = text.slice(eq);
						children[node].add(newText, value);
					} else {
						children[node].add("", value);
					}
					return;
				}
			}
			children[text] = new BTree("", value);
		}
	};
	
	this.get = function(text){
		var starPos = text.indexOf("*");
		text=text.replace(/\*/g, "");
		if(starPos==0){text.split("").reverse().join("");}
		if(!text || text == ""){
			if(starPos<0){
				//return skipList;
				return values;
			}
			else
			{
				var vals = getAllChildValues();
				vals = vals.concat(values);
				vals = removeDuplicateValues(vals);
				//return new SkipList(vals);
				return vals;
			}
		}
		else{
			for(var i in children){
				var eq = equalTo(i, text);
				if(eq>0)
				{
					var pos = text.substr(0, eq);
					var newText = text.slice(eq);
					if(starPos>=0){newText+="*";}
					if(children[pos]){
						return children[pos].get(newText);
					} else {
						//return new SkipList([]);
						return [];
					}
				}
			}
		}
		
	}
	
	this.addChildren = function(node, tree){
		children[node] = tree;
	}
	
	/*this.build = function(){
		skipList = new SkipList(values);
		for(var i in children){
			children[i].build();
		}
	};*/
	
	var getAllChildValues = function(){
		var val = values;
		for(var i in children){
			var c = children[i].getAllChildValues();
			val = val.concat(c);
		}
		return val;
	};
	
	this.getAllChildValues = function(){
		var val = values;
		for(var i in children){
			var c = children[i].getAllChildValues();
			val = val.concat(c);
		}
		return val;
	};
	
	var removeDuplicateValues = function(arr){
		var uniques = [];
		$.each(arr, function(i, el){
			if($.inArray(el, uniques) === -1) uniques.push(el);
		});
		return uniques;
	};
	
	this.print = function(idt){
		/*idt = idt || "";
		
		var sp = "\t";
		
		var text = "";
		text += "{\n";
		if(values.length > 0) text += idt + sp + "[" + values + "]\n";
		for(var i in children){
			text += idt + sp + i + ": " + children[i].print(idt+sp);
		}
		text += idt + "}\n";
		return text;*/
		
		idt = idt || "";
		
		var text = idt;
		if(values.length > 0) text += "[" + values + "]";
		text += "\n";
		for(var i in children){
			var newIdt = idt;
			for(var j = 0; j < i.length; j++) newIdt += " ";
			text += idt + i + " " + children[i].print(newIdt);
		}
		return text;
	};
	
	this.getValues = function(){
		return values;
	};
	
	var equalTo = function(a, b){
		var eq = 0;
		for(var i = 0; i < a.length; i++){
			if(i < b.length){
				if(a.charAt(i) == b.charAt(i)){
					eq++;
				} else {
					break;
				}
			} else {
				break;
			}
		}
		return eq;
	}
			
	if(value != null) this.add(text, value);
};